package cn.tedu.hm.service.impl;

import cn.tedu.hm.ex.ServiceException;
import cn.tedu.hm.mapper.OrderMapper;
import cn.tedu.hm.mapper.RoomMapper;
import cn.tedu.hm.pojo.dto.RoomAddNewDTO;
import cn.tedu.hm.pojo.dto.RoomUpdateDTO;
import cn.tedu.hm.pojo.entity.Room;
import cn.tedu.hm.pojo.vo.RoomListItemVO;
import cn.tedu.hm.pojo.vo.RoomStandardVO;
import cn.tedu.hm.service.IRoomService;
import cn.tedu.hm.wep.JsonPage;
import cn.tedu.hm.wep.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 说明：
 *
 * @author:ZhouYong
 * @Date: 2023/3/11
 **/
@Slf4j
@Service
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;



    public RoomServiceImpl() {
        log.debug("创建业务类对象：RoomServiceImpl");
    }

    @Override
    public void addNew(RoomAddNewDTO roomAddNewDTO) {
        log.debug("开始处理【添加房间】的业务，参数：{}", roomAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的房间的名称
        String name = roomAddNewDTO.getRoomName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = roomMapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加失败,名称被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        // 调用参数对象的getRoomId()得到尝试添加的房间的ID
        Integer roomId= roomAddNewDTO.getRoomId();
        // 调用Mapper对象的countByRoomId()执行统计查询
        int count1 = roomMapper.countByRoomId(roomAddNewDTO.getRoomId());
        log.debug("根据房间Id【{}】统计数量，结果：{}", roomId, count1);
        // 判断统计结果是否大于0
        if (count1 > 0) {
            // 是：ID被占用，抛出异常
            String message = "添加失败,房间id被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        // 创建Room对象
        Room room = new Room();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到Room对象中
        BeanUtils.copyProperties(roomAddNewDTO, room);
        // 调用Mapper对象的insert()执行插入房间数据
        log.debug("开始处理添加房间的请求");
        int rows = roomMapper.insert(room);
        log.debug("开始处理【添加房间】的业务，参数：room{}", room);

        if (rows != 1) {
            String message = "添加失败,服务器忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除房间的业务");
        RoomStandardVO room = roomMapper.getStandardById(id);
        String url = room.getUrl();
        new File("C:\\Users\\Administrator\\IdeaProjects\\web_test\\public\\"+url).delete();
        log.debug("根据ID={}检查房间数据是否存在，查询结果：{}", id, room);
        if (room == null) {
            String message = "删除失败,房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        int rows = roomMapper.deleteById(id);
        if (rows != 1) {
            String message = "添加失败,服务器忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, message);
        }
    }


    @Override
    public void setEnable(Long id) {
        log.debug("开始处理【启用类别】的业务，参数：{}", id);
            updateEnableById(id, 1);
    }


    @Override
    public void setDisable(Long id) {
        log.debug("开始处理【禁用类别】的业务，参数：{}", id);
            updateEnableById(id, 0);
    }

    private void updateEnableById(Long id, Integer enable) {
        // 调用Mapper对象的getStandardById()方法执行查询
        RoomStandardVO roomById = roomMapper.getStandardById(id);
        // 判断查询结果是否为null，如果是，则抛出异常
        if (roomById == null) {
            String message = ENABLE_TEXT[enable] + "房间失败，房间数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable与参数enable是否相同，如果是，则抛出异常（当前状态与目标状态相同，没必要执行更新）
        if (roomById.getEnable() == enable) {
            String message = ENABLE_TEXT[enable] + "房间失败，此房间已经处于" + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        // 创建Room对象
        Room updateRoom = new Room();
        // 向Room对象中封装属性值：id, enable，均来自方法参数
        updateRoom.setId(id);
        updateRoom.setEnable(enable);
        // 调用Mapper对象的update()方法执行更新
        int rows = roomMapper.update(updateRoom);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "房间失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

    }
    @Override
    public void updateInfoById(Long id, RoomUpdateDTO roomUpdateDTO) {
        log.debug("开始处理修改房间的业务");
        RoomStandardVO result = roomMapper.getStandardById(id);
        if (result == null) {
            String message = "修改失败,房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = roomMapper.countByNameAndNotId(id, roomUpdateDTO.getRoomName());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改房间详情失败，房间名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Room room=new Room();
        BeanUtils.copyProperties(roomUpdateDTO,room);
        log.info("room={}",room);
        room.setId(id);
        // 调用Mapper对象的update()方法执行修改
        int rows = roomMapper.update(room);
        if (rows != 1) {
            String message = "添加失败,服务器忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, message);
        }
    }

    @Override
    public JsonPage<RoomStandardVO>getStandardByRoomName(String roomName,Integer page,Integer pageSize) {
        log.debug("开始处理【根据名称查询房间详情】的业务，参数：{}", roomName);
        PageHelper.startPage(page,pageSize);
        List<RoomStandardVO>result =  roomMapper.getStandardByRoomName(roomName,page,pageSize);
        if (result==null){
            String message = "房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonPage.restPage(new PageInfo<>(result));
    }

    @Override
    public JsonPage<RoomStandardVO> getStandardByRoomType(String type, Integer page, Integer pageSize) {
        log.debug("开始处理【根据房间类型查询房间详情】的业务，参数：{}", type);
        PageHelper.startPage(page,pageSize);
        List<RoomStandardVO>result = roomMapper.getStandardByRoomType(type,page,pageSize);
        if (result==null){
            String message = "房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonPage.restPage(new PageInfo<>(result));
    }

    @Override
    public JsonPage<RoomStandardVO> getStandardByRoomNameAndRoomType(String roomName, String type, Integer page, Integer pageSize) {
        log.debug("开始处理【根据房间类型查询房间详情】的业务，参数：{},{}", roomName,type);
        PageHelper.startPage(page,pageSize);
        List<RoomStandardVO> result1 = roomMapper.getStandardByRoomNameAndRoomType(roomName,type,page,pageSize);
        if (result1==null){
            String message = "房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonPage.restPage(new PageInfo<>(result1));
    }


    @Override
    public RoomStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询房间详情】的业务，参数：{}", id);
        RoomStandardVO result = roomMapper.getStandardById(id);
        if (result==null){
            String message = "房间数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return result;
    }


    @Override
    public JsonPage<RoomListItemVO> list(Integer page, Integer pageSize) {
        log.debug("开始处理【查询房间列表】的业务，无参数");
        PageHelper.startPage(page, pageSize);
        List<RoomListItemVO> list = roomMapper.list();
       return JsonPage.restPage(new PageInfo<>(list));
    }
}
